<?php
session_start();
 if (empty($_SESSION['username']) AND empty($_SESSION['passuser'])){
  echo "<link href='style.css' rel='stylesheet' type='text/css'>
 <center>Untuk mengakses modul, Anda harus login <br>";
  echo "<a href=../../index.php><b>LOGIN</b></a></center>";
}
else{
$aksi="modul/mod_anggota/aksi_anggota.php";
$upass = $_SESSION['passuser'];
$gu = mysql_query("SELECT username FROM users WHERE password='$upass'");
$uname = mysql_fetch_object($gu);
$usrnm = $uname->username;

function getGrupName($id) {
	$gr = mysql_query("SELECT nama_grup FROM grup WHERE id_grup='$id'");
	$ro = mysql_fetch_object($gr);
	return $ro->nama_grup;
}
switch($_GET[act]){
  // Daftar Anggota
  default:
    echo "<div class='componentheading'>Daftar Anggota</div>
          <input type=button value='Input Data Anggota' 
          onclick=\"window.location.href='?module=anggota&act=tambahanggota';\">
		  <p>
          <table class='boslist'>
          <tr><th>No</th><th>Nama Lengkap</th><th>Jenis Kelamin</th><th>Asal Kota</th><th>No. HP</th><th>Grup</th><th>Tgl. Daftar</th><th>Aksi</th></tr>"; 
    $tampil=mysql_query("SELECT * FROM anggota ORDER BY id_anggota DESC");
    $no=1;
    while ($r=mysql_fetch_array($tampil)){
       echo "<tr><td align='right'>$no</td>
             <td>$r[nama]</td>
			 <td>$r[jk]</td>
			 <td>$r[asalkota]</td>
			 <td>$r[nohp]</td>
			 <td>".getGrupName($r[id_grup])."</td>
             <td>$r[created]</td>
             <td align='center'><a href=?module=anggota&act=editanggota&id=$r[id_anggota]>Edit</a> | 
	               <a href=$aksi?module=anggota&act=hapus&id=$r[id_anggota]>Hapus</a>
             </td></tr>";
      $no++;
    }
    echo "</table></p>";
    break;
  // Form Input Grup
  case "tambahgrup":
	echo "<div class='componentheading'>Form Input Grup</div>
          <form method=POST action='$aksi?module=anggota&act=addgrup'>
		  <p><strong>Nama Grup :</strong><br />
			<input type='text' name='grupname' />
		  </p>
		  <p>
			<input type='submit' name='submit' value='Simpan' />&nbsp;
            <input type='button' value='Batal' onclick='self.history.back()' />
		  </p>
		  </form>
		 ";
  break;
  // View Grup
  case "grup":
	echo "<div class='componentheading'>Lihat Grup</div>
		  <input type=button value='Tambah Grup' 
          onclick=\"window.location.href='?module=anggota&act=tambahgrup';\">
          <p>
          <table class='boslist'>
          <tr><th>No</th><th>Nama Grup</th><th>Aksi</th></tr>"; 
    $tampil=mysql_query("SELECT * FROM grup ORDER BY id_grup DESC");
    $no=1;
    while ($r=mysql_fetch_array($tampil)){
       echo "<tr><td align='right'>$no</td>
             <td>$r[nama_grup]</td>
             <td align='center'><a href=?module=anggota&act=editgrup&id=$r[id_grup]>Edit</a> | 
	               <a href=$aksi?module=anggota&act=hapusgrup&id=$r[id_grup]>Hapus</a>
             </td></tr>";
      $no++;
    }
    echo "</table></p>";
  break;
  
  // Form Input Data Anggota
  case "tambahanggota":
    echo "<div class='componentheading'>Form Input Data Anggota</div>
          <form method=POST action='$aksi?module=anggota&act=input'>
		  <table class='bosform'>
			<tr>
				<td>Nama Lengkap</td>
				<td>:</td>
				<td>
					<input type='text' name='nama' />
				</td>
			</tr>
			<tr>
				<td>Jenis Kelamin</td>
				<td>:</td>
				<td>
					<input type='radio' name='jk' value='L' checked='checked'/>&nbsp;Laki-laki&nbsp;&nbsp;
					<input type='radio' name='jk' value='P' />&nbsp;Perempuan
				</td>
			</tr>
			<tr>
				<td>Asal Kota</td>
				<td>:</td>
				<td>
					<input type='text' name='kota' />
				</td>
			</tr>
			<tr>
				<td>No. HP</td>
				<td>:</td>
				<td>
					<input type='text' name='nohp' />
				</td>
			</tr>
			<tr>
				<td>Pilih Grup</td>
				<td>:</td>
				<td>
					<select name='grup'>
						<option value=''>-- Pilih Grup --</option>
			";
			$qra = mysql_query("SELECT * FROM grup");
			while($rg = mysql_fetch_array($qra)) {
				echo "<option value='".$rg[id_grup]."'>".$rg[nama_grup]."</option>";
			}
			echo "
					</select>
				</td>
			</tr>
		  </table>
		  <p>
			<input type='submit' name='submit' value='Simpan' />&nbsp;
            <input type='button' value='Batal' onclick='self.history.back()' />
		  </p>
          </form>";
     break;
  
  // Form Edit Data Anggota 
  case "editanggota":
    $edit=mysql_query("SELECT * FROM anggota WHERE id_anggota='$_GET[id]'");
    $r=mysql_fetch_array($edit);
	if(!empty($r[jk])) {
		if($r[jk] == 'L') {
			$lck = "checked='checked'";
		}
		elseif($r[jk] != 'L') {
			$lck = "";
			if($r[jk] == 'P') {
				$pck = "checked='checked'";
			}
			else {
				$pck = "";
			}
		}
	}
    echo "<div class='componentheading'>Edit Data Anggota</div>
          <form method=POST action=$aksi?module=anggota&act=update>
          <input type=hidden name=id value='$r[id_anggota]'>
		  <table class='bosform'>
			<tr>
				<td>Nama Lengkap</td>
				<td>:</td>
				<td>
					<input type='text' name='nama' value='".$r[nama]."' />
				</td>
			</tr>
			<tr>
				<td>Jenis Kelamin</td>
				<td>:</td>
				<td>
					<input type='radio' name='jk' value='L' ".$lck." />&nbsp;Laki-laki&nbsp;&nbsp;
					<input type='radio' name='jk' value='P' ".$pck." />&nbsp;Perempuan
				</td>
			</tr>
			<tr>
				<td>Asal Kota</td>
				<td>:</td>
				<td>
					<input type='text' name='kota' value='".$r[asalkota]."' />
				</td>
			</tr>
			<tr>
				<td>No. HP</td>
				<td>:</td>
				<td>
					<input type='text' name='nohp' value='".$r[nohp]."' />
				</td>
			</tr>
			<tr>
				<td>Grup</td>
				<td>:</td>
				<td>
					<select name='grup'>
						<option value=''>-- Pilih Grup --</option>
			";
			$eg = mysql_query("SELECT * FROM grup");
			while($rgu = mysql_fetch_array($eg)) {
				echo "<option value='".$rgu[id_grup]."'";
				if($rgu[id_grup] == $r[id_grup]) $sel = " selected='selected'";
				else $sel = "";
				echo $sel;
				echo " >".$rgu[nama_grup]."</option>";
			}
			echo "
					</select>
				</td>
			</tr>
		  </table>
		  <p>
			<input type='submit' name='submit' value='Simpan' />&nbsp;
            <input type='button' value='Batal' onclick='self.history.back()' />
		  </p></form>";
    break;  
}
}
?>
